package com.skydroid.serialport

import android.content.Context
import android.content.Intent
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.skydroid.fpvlibrary.common.connect.serialport.SerialPortConnection
import com.skydroid.fpvlibrary.common.video.FPVVideoClient
import com.skydroid.fpvlibrary.ui.widget.uartvideo.GLHttpVideoSurface
import java.io.File

class SerialPortSamplesActivity: AppCompatActivity() {
    private var mPreviewDualVideoView: GLHttpVideoSurface? = null

    private var mSerialPortConnection: SerialPortConnection? = null

    private var fPVVideoImp : FPVVideoClient? = null

    private var context : Context? = null

    val mainHanlder: Handler by lazy { Handler(Looper.getMainLooper()) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_samples)
        context = this
        initView()
        initData()
    }

    companion object{
        fun start(context : Context?){
            context?.startActivity(Intent(context,SerialPortSamplesActivity::class.java))
        }
    }

    private fun initView(){
        mPreviewDualVideoView = findViewById(R.id.video)

        findViewById<View>(R.id.startRecord).setOnClickListener {
            fPVVideoImp?.startRecord(null,null)
        }
        findViewById<View>(R.id.stopRecord).setOnClickListener {
            fPVVideoImp?.stopRecord()
        }
        findViewById<View>(R.id.captureSnapshot).setOnClickListener {
            fPVVideoImp?.captureSnapshot(null,null)
        }
    }

    private fun initData(){
        mPreviewDualVideoView?.init()
        mSerialPortConnection =
            SerialPortConnection(
                921600,
                "/dev/ttyHS0"
            )
        mSerialPortConnection?.setDelegate(object : SerialPortConnection.Delegate{
            override fun received(bytes: ByteArray?, size: Int) {
                fPVVideoImp?.received(bytes,size)
            }
        })
        fPVVideoImp = FPVVideoClient()
        fPVVideoImp?.setRun(mSerialPortConnection?.isConnection()?:false)
        fPVVideoImp?.setDelegate(object :FPVVideoClient.Delegate{
            override fun onStopRecordListener(fileName: String?) {
                runOnUiThread {
                    sendBroadcast(
                        Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                            Uri.fromFile(File(fileName))
                        )
                    )
                    MediaScannerConnection.scanFile(
                        context,
                        arrayOf(fileName),
                        null,
                        null
                    )
                    Toast.makeText(
                        context,
                        fileName,
                        Toast.LENGTH_LONG
                    ).show()
                }
            }

            override fun onSnapshotListener(fileName: String?) {
                runOnUiThread {
                    sendBroadcast(
                        Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                            Uri.fromFile(File(fileName))
                        )
                    )
                    MediaScannerConnection.scanFile(
                        context,
                        arrayOf(fileName),
                        null,
                        null
                    )
                    Toast.makeText(
                        context,
                        fileName,
                        Toast.LENGTH_LONG
                    ).show()
                }
            }

            override fun renderI420(frame: ByteArray?, width: Int, height: Int) {
                mPreviewDualVideoView?.renderI420(frame, width?:0, height?:0)
            }


            override fun resetView() {
                mPreviewDualVideoView?.resetView(mainHanlder)
            }

            override fun setVideoSize(picWidth: Int, picHeight: Int) {
                mPreviewDualVideoView?.setVideoSize(picWidth?:0, picHeight?:0,mainHanlder)

            }
        })
        fPVVideoImp?.startPlayback()
//        var fpvClient = FPVClient(mSerialPortConnection)
    }

    override fun onDestroy() {
        super.onDestroy()
        mSerialPortConnection?.closeConnection()
        fPVVideoImp?.stopPlayback()
    }
}